<template>
  <c-dialog ref="dialog" :data="selData" single title="资料审核" @closed="closedHandle">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="80px">
      <c-list-item prop="dataReviewResult" label="审核结果">
        <el-select v-model="formData.dataReviewResult" placeholder="审核结果" clearable>
          <el-option label="同意" :value="1" />
          <el-option label="不同意" :value="0" />
        </el-select>
      </c-list-item>
      <c-list-item prop="opinion" label="审核意见">
        <el-input v-model="formData.opinion" type="textarea" placeholder="审核意见" maxlength="1024" show-word-limit :autosize="{minRows:3}" clearable />
      </c-list-item>
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">保存</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { postAudit } from '@/api/decorate'
export default {
  name: 'Audit',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    const validatorOpinion = (r, v, c) => {
      if (this.formData.dataReviewResult === 0 && (v === '' || v === undefined)) {
        c(new Error('请输入审核意见'))
      } else {
        c()
      }
    }
    return {
      formData: {
        dataReviewResult: '',
        opinion: ''
      },
      formRules: {
        dataReviewResult: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
        opinion: [{ validator: validatorOpinion, trigger: 'change' }]
      }
    }
  },
  computed: {
    detail() {
      return this.selData.length === 1 ? this.selData[0] : {}
    }
  },
  methods: {
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const keys = 'D3:approachDocAuditAdvice E2:hideProAuditAdvice F2:resizeAuditAdvice G2:firstCheckAuditAdvice'.split(' ')
        let opinionKey
        for (const key of keys) {
          const [k, v] = key.split(':')
          if (k === this.detail.steps) {
            opinionKey = v
            break
          }
        }
        if (opinionKey === undefined) return
        const data = {
          id: this.detail.id,
          dataReviewResult: this.formData.dataReviewResult,
          steps: this.detail.steps// 必填
        }
        data[opinionKey] = this.formData.opinion
        return postAudit(data).then(res => {
          this.$message.success('操作成功！')
          this.$emit('on-success')
          this.$refs.dialog.visible = false
        })
      })
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    },
    closedHandle() {
      this.$refs.form.resetFields()
    }
  }
}
</script>
